//
//  ContributionScoreObj.m
//  CareRingApp
//
//  Created by Linktop on 2022/8/3.
//  用于 就绪 + 睡眠的贡献评分计算

#import "ContributionScoreObj.h"
#import "DBTables.h"

#import "NSDictionary+SleepUsed.h"
#import "SleepStageHeader.h"
#import "StagingDataV2.h"
@implementation ContributionScoreObj


//+(UIColor *)levelColor:(CONTRIBUTE_LEVEL)level
//{
//    if (level == CONTRIBUTE_LEVEL_BEST) {
//        return HEALTH_COLOR_BEST;
//    }
//    
//    if (level == CONTRIBUTE_LEVEL_GOOD) {
//        return HEALTH_COLOR_WELL;
//    }
//    if (level == CONTRIBUTE_LEVEL_ATTENTION) {
//        return HEALTH_COLOR_ATTECTION;
//    }
//    return nil;
//}


/// 计算睡眠效率
/// - Parameter sleepData:
+(NSNumber *)calcSleepEffecy:(StagingDataV2 *)sleepData
{
    
    NSTimeInterval sleepTime = [[self calcSleepDuration:sleepData] doubleValue];
    __block NSTimeInterval sleepTimeTemp = sleepTime;
    [sleepData.ousideStagingList enumerateObjectsUsingBlock:^(StagingSubObj * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.type == NONE || obj.type == WAKE) {
            NSMutableArray<StagingListObj *> * dictArray = obj.list;
            NSTimeInterval diff = fabs([dictArray.firstObject.time doubleValue] - [dictArray.lastObject.time doubleValue]);
            
            
            sleepTimeTemp -= diff;
        }
    }];
    NSNumber * sleepEffectPercent = nil;
    if (sleepTime != 0) {
        sleepEffectPercent = @(sleepTimeTemp / sleepTime);
    }
    return sleepEffectPercent;
}

/// 计算优质睡眠时间
/// - Parameter sleepObj: 睡眠
+(NSNumber *)calcQualitySleepDuration:(StagingDataV2 *)sleepObj
{
    NSTimeInterval qulaityTime = 0;
    int count = 0;
    
    for (int i  = 0; i < sleepObj.ousideStagingList.count; i++) {
          StagingSubObj *dict = sleepObj.ousideStagingList[i];
          if (dict.type == REM || dict.type == NREM1 || dict.type == NREM2) {
              NSTimeInterval tempInteval;
              if (i < sleepObj.ousideStagingList.count - 1) {
                  tempInteval = fabs(dict.list.firstObject.time.doubleValue
                                     -  sleepObj.ousideStagingList[i+1].list.firstObject.time.doubleValue);
                  qulaityTime += tempInteval;
              } else {
                  tempInteval = fabs(dict.list.firstObject.time.doubleValue
                                     -  dict.list.lastObject.time.doubleValue);
                  qulaityTime += tempInteval;
              }
              count++;
          }
      }
    return @(qulaityTime);
}

+(NSNumber *)calcSleepDuration:(StagingDataV2 *)sleepObj
{
    return @(sleepObj.endTime - sleepObj.startTime);
}

+(NSNumber *)calcDeepSleepDuration:(StagingDataV2 *)sleepObj
{
    NSTimeInterval sleepTime = sleepObj.endTime - sleepObj.startTime;
    NSTimeInterval deepTime = 0;
    for (int i  = 0; i < sleepObj.ousideStagingList.count; i++) {
        StagingSubObj *dict = sleepObj.ousideStagingList[i];
        if (dict.type == NREM3) {
            NSTimeInterval tempInteval = dict.list.lastObject.time.doubleValue
            -  dict.list.firstObject.time.doubleValue;
            deepTime += tempInteval;
            
        }
    }
    return @(deepTime);
    
}


@end
